<!DOCTYPE html>
<html lang="zh-cn">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>糕点 -- 首页</title>
    <!-- <script src="{{ url_for('static',filename='js/jquery.min.js') }}"></script>
        <link rel="stylesheet" href="{{ url_for('static',filename='css/style.css') }}">
        <script src="{{ url_for('static',filename='js/config.js') }}"></script>
        <script src="{{ url_for('static',filename='js/loginApp.js') }}"></script>
        <script src="{{ url_for('static',filename='js/jquery.cookie.min.js') }}"></script> -->

    <!-- 以下内容必须在每个页面都有 -->
    <link rel="shortcut icon" href="./favicon.ico" type="image/x-icon">
    <script src="./static/js/jquery.js"></script>
    <script src="./static/js/jquery.cookie.js"></script>
    <script src="./static/js/config.min.js"></script>
    <script src="./static/js/index.js"></script>
    <!-- 以上内容必须在每个页面都有 -->
    <style>
        footer {
            /* border-top:1px solid #000; */
            padding: 20px 0 30px 0;
            width: 100%
        }

        #loginApp,
        #logout,
        #networkQuitAll {
            margin-top: 20px;
        }

        #logout {
            background-color: #df3a3a;
        }

        #logout:hover {
            background-color: #ec3e4d;
        }

        #networkQuitAll {
            background-color: #0ec5e6;
        }

        #networkQuitAll:hover {
            background-color: #0cb8d6;
        }

        #avatar {
            height: 128px;
            width: 128px;
            margin-top: 30px;
            border-radius: 50%;
            -webkit-user-drag: none;
        }
    </style>

    <script>
        $(document).ready(function () {
            // _ip = myip();
            _ip = $.cookie("myip");
            closeMsg = Dreamer.loading("载入中……");
            networkQuery(
                str = _ip,
                success = function (code, msg) {
                    if (code == 200) {
                        if (msg == 400) {
                            $('#loginApp').text("连接网络");
                        } else {
                            $('#loginApp').text("断开网络");
                        };
                    } else { Dreamer.error("未知错误，已打印控制台！", 2000, true) };

                }
            );
            user(
                unique_number = $.cookie('userid'),
                success = function (code, msg, result) {
                    // let avatar = "./static/svg/logo.svg";
                    if (code == 200) {
                        let avatar = result.avatar;
                        document.getElementById("avatar").setAttribute("src", avatar);
                        Dreamer.info("欢迎回来，" + result.nickname);
                    };
                    closeMsg();
                }
            );
            $('#logout').click(function () {
                let closeMsg = Dreamer.loading("退出登录中，请稍后。");
                logout(function (code, msg) {
                    closeMsg();
                    if (code == 200) {
                        let result = $.removeCookie("token", { path: '/' });
                        console.log(result);
                        Dreamer.success(msg, 2000, true, function () {
                            window.location.href = "./login.html";
                        });
                    } else {
                        Dreamer.error(msg, 2000, true);
                    };
                });
            });
            $('#loginApp').click(function () {
                networkQuery(
                    str = _ip,
                    success = function (code, msg) {
                        if (code == 200) {
                            if (msg == 400) {
                                let closeMsg = Dreamer.loading("认证网络中，请稍后。");
                                loginApp(
                                    str = _ip,
                                    success = function (code, msg) {
                                        closeMsg();
                                        if (code == 200) {
                                            $('#loginApp').text("断开网络");
                                            Dreamer.success(msg, 2000, true);
                                        } else {
                                            Dreamer.error(msg, 2000, true);
                                        };
                                    }
                                );

                            } else {
                                btn_title = $('#loginApp').text();
                                if (btn_title == "连接网络") {
                                    Dreamer.error("当前设备已连上网络！", 2000);
                                } else if (btn_title == "断开网络") {
                                    let closeMsg = Dreamer.loading("断开网络中，请稍后。");
                                    networkBreak(
                                        ip = _ip,
                                        success = function (code, msg) {
                                            if (code == 200) {
                                                closeMsg();
                                                $('#loginApp').text("连接网络");
                                                Dreamer.success(msg, 2000, true);
                                            } else {
                                                Dreamer.error(msg, 2000, true)
                                            }
                                        }
                                    );
                                }
                            };
                        } else {
                            Dreamer.error("未知错误，已打印控制台！", 2000)
                            console.log("code: " + code + "msg: " + msg)
                        }

                    }
                );
            });
            $("#networkQuitAll").click(function () {
                let closeMsg = Dreamer.loading("获取在线设备中……");
                network(
                    success = function (code, msg, result) {
                        
                        if (200 == code && result.length > 0) {
                            // 如果code == 200 and result.length > 0则有设备在线
                            // result是一个数组 [{"str": string, "type": number}]
                            for (let i = 0; i < result.length; i++) {
                                networkBreak(result[i].str, function(code, msg){ }, false);
                            }
                            Dreamer.info("已经断开全部设备！", 2000 ,true);
                        } else {
                            Dreamer.info("没有在线设备。", 2000);
                        }
                        closeMsg();
                    }
                );
            });
        });

    </script>
</head>

<body>
    <div class="bgdiv">
        <header>
            <!-- 头部 -->

        </header>
        <section>
            <!-- 主体内容 -->
            <img src="./static/svg/logo.svg" id="avatar">
            <div>
                <button id="loginApp" class="btn">连接网络</button>
                <button id="logout" class="btn">退出登录</button>
                <button id="networkQuitAll" class="btn">断开所有设备</button>
            </div>
        </section>
        <article>
            <!-- 其余补分 -->
        </article>
    </div>
    <footer>
        <!-- 尾部 -->
        <address>
            贡献者:
            <a href="https://gitee.com/baiyu16">白羽 </a>
            <a href="https://gitee.com/huang999">寂寞如斯</a>
        </address>
    </footer>
</body>

</html>